/*******************************************************************************
Create summary statistic tables

********************************************************************************/
set more off
use "${data}\UgandaKenya20162019Replication.dta", clear

*------------------------------------------------------------------------------*/
eststo clear
					
******* KENYA *******
*** 1. Outcomes before ***
la var defor10 "Forest loss 10\% threshold, before ban"
la var defor30 "Forest loss 30\% threshold, before ban"
la var defor50 "Forest loss 50\% threshold, before ban"

estpost sum `outcomes'  if pa==1 & country == "Kenya" & postban == 0
est sto A1
	
// non-PA 
estpost sum `outcomes'  if pa==0 & country == "Kenya"  & postban == 0
est sto B1
	
// PA - Difference
preserve
foreach var in `outcomes' {
	ttest `var' if country == "Kenya" & postban == 0, by (pa) 
	replace `var' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2)) if country == "Kenya" & postban == 0
}

estpost sum `outcomes' if country == "Kenya" & postban == 0
estimates store C1
restore


estout A1 B1 C1 using "${tables}\KenyaSumstats.tex", replace ///
	cells("mean(fmt(%12.2fc))") label style (tex) ///
	prehead( \begin{tabular}{lccc}    ///
	 \hline \hline ///
	& Protected Areas & non-Protected Areas & Norm. Diff. \\ ///
	\hline ) ///
	collabels(none)  mlabels(none) ///
	end(\\ \addlinespace) postfoot()



*** 2. Covariates and outcomes after ***

la var defor10 "Forest loss 10\% threshold, after ban"
la var defor30 "Forest loss 30\% threshold, after ban"
la var defor50 "Forest loss 50\% threshold, after ban"

// PA 
estpost sum `outcomes' `covariates' if pa==1 & country == "Kenya" & postban == 1
est sto A2
	
// non-PA 
estpost sum `outcomes' `covariates' if pa==0 & country == "Kenya" & postban == 1
est sto B2	
	
// PA - Difference
preserve
foreach var in `outcomes' `covariates' {
	ttest `var' if country == "Kenya" & postban == 1, by (pa) 
	replace `var' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2)) if country == "Kenya" & postban == 1
}

estpost sum `outcomes' `covariates' if country == "Kenya" & postban == 1
estimates store C2
restore
	
// Create table for outcomes:
estout A2 B2 C2 using "${tables}\KenyaSumstats.tex", append ///
	cells("mean(fmt(%12.2fc))") label style (tex) ///
	prehead(  )  ///
	mlabels( none)  collabels(none) stats(N, labels("Obs 10\% threshold")  fmt(%12.0gc)) ///
	end(\\ \addlinespace) postfoot(\hline \hline \end{tabular} ///
	\begin{tablenotes}[para,flushleft] ///
	\footnotesize{Columns (1) and (2) show mean values of covariates  ///
	for grid cells inside of PAs, and outside of PAs. Column (3) presents ///
	the normalized difference between these two.} \end{tablenotes} )

******* UGANDA *******
*** 1. Outcomes before ***
la var defor10 "Forest loss 10\% threshold, before ban"
la var defor30 "Forest loss 30\% threshold, before ban"
la var defor50 "Forest loss 50\% threshold, before ban"


estpost sum `outcomes'  if near_bordc==1 & postban==0 & country == "Uganda"
	
// near border
estpost sum `outcomes'  if near_bordc==0 & postban==0 & country == "Uganda"
est sto B1
	
//far from border
preserve
foreach var in `outcomes' {
	ttest `var' if country == "Uganda" & postban==0, by (near_bordc) 
	replace `var' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2)) if country == "Uganda" & postban==0
}

estpost sum `outcomes' if country == "Uganda" & postban==0
estimates store C1
restore


estout A1 B1 C1 using "${tables}\UgandaSumstats.tex", replace ///
	cells("mean(fmt(%12.2fc))") label style (tex) ///
	prehead( \begin{tabular}{lccc}    ///
	 \hline \hline ///
	& Near border & Far from border & Norm. Diff. \\ ///
	\hline ) ///
	collabels(none)  mlabels(none) ///
	end(\\ \addlinespace) postfoot()



*** 2. Covariates and outcomes after ***

la var defor10 "Forest loss 10\% threshold, after ban"
la var defor30 "Forest loss 30\% threshold, after ban"
la var defor50 "Forest loss 50\% threshold, after ban"


// Near 
estpost sum `outcomes' `covariates' if near_bordc==1 & postban==1 & country == "Uganda" 
est sto A2
	
// far 
estpost sum `outcomes' `covariates' if near_bordc==0 & postban==1 & country == "Uganda"
est sto B2	
	
// Difference
preserve
foreach var in `outcomes' `covariates' {
	ttest `var' if country == "Uganda" & postban == 1, by(near_bordc) 
	replace `var' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2)) if country == "Uganda" & postban==1 
}

estpost sum `outcomes' `covariates' if  country == "Uganda" & postban==1 
estimates store C2
restore
	
// Create table for outcomes:
estout A2 B2 C2 using "${tables}\UgandaSumstats.tex", append ///
	cells("mean(fmt(%12.2fc))") label style (tex) ///
	prehead(  )  ///
	mlabels( none)  collabels(none) stats(N, labels("Obs 10\% threshold")  fmt(%12.0gc)) ///
	end(\\ \addlinespace) postfoot(\hline \hline \end{tabular} ///
	\begin{tablenotes}[para,flushleft] ///
	\footnotesize{Columns (1) and (2) show mean values of covariates  ///
	less than and greater than 100 km to a Kenyan border crossing. Column (3) presents ///
	the normalized difference between these two.} \end{tablenotes} )

